package com.example.demo.mapper;

import com.example.demo.bean.Goods;
import com.example.demo.bean.view.GoodsView;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository("GoodsMapper")
public interface GoodsMapper {
    //插入商品
    @Insert("insert into goods(goodsNum, sellerNum, goodsType, goodsName, Imag, GoodsIntroduction, price) " +
            "value(#{goodsNum}, #{sellerNum}, #{goodsType}, #{goodsName},#{imagUrl}, #{goodsIntroduction}, #{price})")
    public boolean insertGoods(String goodsNum, String sellerNum, String goodsType, String goodsName, String imagUrl, String goodsIntroduction, int price);

    //通过商品编号删除商品
    @Delete("delete from goods where " +
            "goodsNum = #{goodsNum}")
    public boolean deleteGoodsById(String goodsNum);

    //更新商品信息，通过商品编号更新
    @Update("update goods " +
            "set goodsType = #{goodsType}, goodsName = #{goodsName}, imag = #{imagUrl}, goodsIntroduction = #{goodsIntroduction}, price = #{price} " +
            "where goodsNum = #{goodsNum}")
    public boolean updateGoods(String goodsNum, String goodsType, String goodsName, String imagUrl, String goodsIntroduction, int price);

    //查询所有商品
    @Select("select * from goods")
    public List<Goods> findAllGoods();

    @Select("select goods.goodsNum, goods.sellerNum, goods.imag, " +
            "goods.goodsIntroduction, goods.price, user.member " +
            "from goods, User " +
            "where user.userNum = goods.sellerNum ")
    public List<GoodsView> findAllGoodsView();
    //查询某一个商品
    @Select("SELECT  goods.goodsNum, goods.sellerNum, goods.goodsIntroduction, goods.goodsName, " +
            "        goods.price, goods.goodsType, user.member ,goods.imag " +
            "FROM User, goods " +
            "WHERE user.userNum = goods.sellerNum and goods.goodsNum = #{goodsNum}")
    public GoodsView findGoodsById(String goodsNum);

    //查询某个人的所有商品
    @Select("SELECT  goods.goodsNum, goods.sellerNum, goods.goodsIntroduction, goods.goodsName, " +
            "        goods.price, goods.goodsType,  user.member ,goods.imag " +
            "FROM User, goods " +
            "WHERE user.userNum = #{sellerNum} and user.userNum = goods.sellerNum ")
    public List<GoodsView> findGoodsByOwner(String sellerNum);

    @Select("SELECT  goods.goodsNum, goods.sellerNum, goods.goodsIntroduction, goods.goodsName, " +
            "        goods.price, goods.goodsType,  user.member ,goods.imag " +
            "FROM User, goods " +
            "where user.userNum = goods.sellerNum and goodsName like concat('%', #{goodsName}, '%')")
    public List<GoodsView> findGoodsByName(String goodsName);
}
